package com.bw.services.car_manage

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import com.bw.lib_base.base.BaseActivity
import com.bw.services.R
import com.bw.services.adapter.Car_NameAdpter
import com.bw.services.databinding.ActivityAddcarBinding
import com.bw.services.viewmodel.AddcarViewModel
import com.tencent.mmkv.MMKV
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.time.LocalDate
import java.time.ZoneId
@Route(path = "services/AddcarActivity")
@AndroidEntryPoint
class AddcarActivity : BaseActivity<ActivityAddcarBinding, AddcarViewModel>() {

    var username= MMKV.defaultMMKV().decodeString("username")
    var MyuserId= MMKV.defaultMMKV().decodeInt("my_userId")


    val currentDate= LocalDate.now(ZoneId.systemDefault())
    var month=0

    val vehicleNameAdpter by lazy { Car_NameAdpter() }
    var sb:StringBuilder=StringBuilder()


    override fun generatePremission(pageName: String): Array<String> {
        TODO("Not yet implemented")
    }

    override fun setupUI() {
        binding.vehicleNameRv.apply {
            adapter=vehicleNameAdpter
            layoutManager= GridLayoutManager(context,7)
        }

        vehicleNameAdpter.setOnItemClickListener { adapter, view, position ->
            val newEditable = Editable.Factory.getInstance().newEditable(vehicleNameAdpter.getItem(position).abbreviation)
            binding.tvJm.text=newEditable

        }


        binding.btnAdd.setOnClickListener {

//            var add=AddVehicle()
//            add.id=MyuserId
//            add.userId=MyuserId
//            add.ownerName=username!!
//            add.licensePlate=sb.toString()
//            add.provinceAbbreviation=binding.editFirst.text.toString()
//            vm.addVehicle(add)
        }



    }

    override fun fetchData() {
        vm.getVehicleName()
    }

    override fun observeData() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED,{
                vm.addNameServiceData.collect{
                    if(it!=null){
                        vehicleNameAdpter.data.clear()
                        vehicleNameAdpter.addData(it)
                        vehicleNameAdpter.notifyDataSetChanged()
                    }else{
                        Log.i("===","各省请求为空")
                    }
                }
            })
        }
    }

    override fun handleEvent() {

    }

    override val mLayoutResId: Int
        get() = R.layout.activity_addcar

}